package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.PermissionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 权限管理
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Reference//查询服务
    private PermissionService permissionService;

    //新增权限
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission) {
        try {
            permissionService.add(permission);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false,"添加权限失败");
        }
        return new Result(true, "添加权限成功");
    }

    //权限分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = permissionService.pageQuery(queryPageBean);
        return pageResult;
    }

    //删除权限
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
             List<Role> role=permissionService.findRoleById(id);
            if (role!=null&&role.size()>0) {
                ArrayList<String> strings = new ArrayList<>();
                for (Role role1 : role) {
                    strings.add(role1.getName());
                }
                return new Result(false, strings+"已关联此权限，无法删除");
            }
            permissionService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, "删除失败");
        }
        return new Result(true,"删除成功");
    }

    //根据id查询数据
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Permission permission = permissionService.findById(id);
            return new Result(true,"查询权限失败", permission);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, "查询权限成功");
        }

    }

    //根据id查询数据
    @RequestMapping("/findRoleById")
    public Result findRoleById(Integer id) {
        try {
            List<Role> list = permissionService.findRoleById(id);
            if (list!=null&&list.size()>0) {
                ArrayList<String> strings = new ArrayList<>();
                for (Role role1 : list) {
                    strings.add(role1.getName());
                }
                return new Result(true, strings.toString());
            }
            return new Result(true,"查询权限成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, "查询权限失败");
        }

    }

    //编辑数据
    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission) {
        try {
            permissionService.edit(permission);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, "权限编辑失败");
        }
        return new Result(true, "编辑权限成功");
    }

    //查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Permission> list = permissionService.findAll();
            return new Result(true,"查询所有权限成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询所有权限失败");
        }
    }
}

















